import cv2 as cv
import sys
import os
from python_ai.common.xcommon import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime

pic_dir = '../../../../../large_data/pic/'
# pic = os.path.join(pic_dir, 'messi_large.jpg')
pic = os.path.join(pic_dir, 'messi5.jpg')
A = cv.imread(pic, cv.IMREAD_COLOR)
A = cv.resize(A, None, fx=0.5, fy=0.5, interpolation=cv.INTER_CUBIC)
# A = cv.blur(A, (5, 5))
A_shape = A.shape
H = A_shape[0]
W = A_shape[1]
H2 = H // 2
W2 = W // 2
print(f'H {H}, W {W}')
print(f'H2 {H2}, W2 {W2}')
right_mat = np.full((H, W2, 3), 0, dtype=np.uint8)
all = np.concatenate((A, right_mat), axis=1)
print(f'all.shape = {all.shape}')

A2 = cv.pyrDown(A)
print(f'A2.shape = {A2.shape}')
A2H = A2.shape[0]
A2W = A2.shape[1]
all[0:A2H, W:W + A2W] = A2

A3 = cv.pyrDown(A2)
print(f'A3.shape = {A3.shape}')
A3H = A3.shape[0]
A3W = A3.shape[1]
all[H2:H2 + A3H, W:W + A3W] = A3

A4 = cv.pyrDown(A3)
print(f'A4.shape = {A4.shape}')
A4H = A4.shape[0]
A4W = A4.shape[1]
all[H2 + A3H: H2 + A3H + A4H, W:W + A4W] = A4

cv.imshow('messi pyrDown', all)

L = A - cv.pyrUp(A2)
L = cv.cvtColor(L, cv.COLOR_BGR2GRAY)

right_mat = np.full((H, W2), 0, dtype=np.uint8)
all2 = np.concatenate((L, right_mat), axis=1)

A3up = cv.pyrUp(A3)
L2 = A2 - cv.resize(A3up, (A2.shape[1], A2.shape[0]), interpolation=cv.INTER_CUBIC)
L2 = cv.cvtColor(L2, cv.COLOR_BGR2GRAY)
all2[0:A2H, W:W + A2W] = L2

A4up = cv.pyrUp(A4)
L3 = A3 - cv.resize(A4up, (A3.shape[1], A3.shape[0]), interpolation=cv.INTER_CUBIC)
L3 = cv.cvtColor(L3, cv.COLOR_BGR2GRAY)
all2[H2:H2 + A3H, W:W + A3W] = L3

cv.imshow('messi pyrUp', all2)
